
public class Tree {
	Node _Root = null;
	Board _Board;
	int _value;
	boolean error;
	int _Depth;
	Player _Player;
	
	Tree (Board board, Player player, int depth, int move) {
		_Board = board;
		_Depth = depth;
		_Player = player;
		generateTree(move);
	}
	
	int alphabeta(Node node, int depth, int a, int b, Player player) {
		if (depth <= 0 || node._child.length == 0) {
			return node.getValue(player);
		}
		
		if (player == Player.TWO) {
			for (int i=0; i<node._child.length; ++i) {
				a = Math.max(a, alphabeta(node._child[i], depth-1, a, b, node.alternatePlayer(player)));
				if (b <= a) return a;
			}
			return a;
		} else {
			for (int i=0; i<node._child.length; ++i) {
				b = Math.min(b, alphabeta(node._child[i], depth-1, a, b, node.alternatePlayer(player)));
				if (b <= a) return b;
			}
			return b;
		}
	}
	
	void generateTree(int move) {		
		_Root = new Node();
		_Root._board = _Board;
		_Root._moves = new byte[1];
		_Root._moves[0] = (byte)move;
		
		_Root.generateChildren(_Depth);
	}
	
	int getValue() {
		_value = alphabeta(_Root, _Depth, Integer.MIN_VALUE, Integer.MAX_VALUE, Player.ONE);
		return _value;
	}
}
